package od_2025A.one;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * TODO 和答案的解题思路不一致；答案是通过动态规划去解决的
 * https://hydro.ac/d/coder_gather/p/oda0002
 * 示例：
 * 输入：
 * hello123world
 * hello123abc4
 * 输出：
 * hello123
 */
public class Test03 {

    public static void main(String[] args) {
        // 1、获取输入的两行数据
        Scanner scanner = new Scanner(System.in);
        String firstLine = scanner.nextLine();
        String secondLine = scanner.nextLine();
        if (firstLine.isEmpty()|| secondLine.isEmpty()){
            System.out.println("");
        }
        int length = Math.min(firstLine.length(), secondLine.length());
        String string = findSameChildStr(firstLine, secondLine, length);
        System.out.println(string);
    }


    private static String findSameChildStr(String firstLine, String secondLine, int length) {
        if (length == 0) {
            return "";
        }
        // 2、比较两行数据的长度，以长度较短的为子串的初始长度
        List<String> firstChildrenStr = new ArrayList<>();
        for (int i = 0; i <= (firstLine.length() - length); i++) {
            firstChildrenStr.add(firstLine.substring(i, i + length));
        }
        List<String> secondChildrenStr = new ArrayList<>();
        for (int i = 0; i <= (secondLine.length() - length); i++) {
            secondChildrenStr.add(secondLine.substring(i, i + length));
        }
        String string = find(firstChildrenStr, secondChildrenStr);
        if (Objects.nonNull(string)) {
            return string;
        }
        length = length - 1;
        return findSameChildStr(firstLine, secondLine, length);
    }


    private static String find(List<String> firstChildrenStr, List<String> secondChildrenStr) {
        for (String s : firstChildrenStr) {
            for (String s1 : secondChildrenStr) {
                if (s.equals(s1)) {
                    return s;
                }
            }
        }
        return null;
    }
}
